# coding:utf-8
from rest_framework import serializers
from goods_info.models import GoodsCategoryModel


class GoodsCategorySerializer(serializers.ModelSerializer):
    """
    商品类别的序列化器和反序列化
    """
    #为了显示树形类别结构
    children = serializers.SerializerMethodField(read_only=True)
    class Meta:
        model = GoodsCategoryModel
        fields = "__all__"

    def get_children(self,obj): #obj是当前模型对象，GoodsCategoryModel对象
        if obj.children:
            return GoodsCategorySerializer(obj.children,many=True).data
        else:
            return None
